//
//  DeviceSNUpdateNameVC.swift
//  kxf
//
//  Created by LNS2 on 2024/4/21.
//

import Foundation
import MCToast

class DeviceSNUpdateNameVC : WHBaseViewVC {
    
    var oldName = ""
    var deviceId = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
    }
    lazy var titleVm: RegisterViewHeadTitleVM = {
        let vm = RegisterViewHeadTitleVM.init(frame: CGRect.init(x: 0, y: getNavigationBarHeight()+kFitWidth(10), width: SCREEN_WIDHT, height: kFitWidth(44)))
        vm.titleLabel.text = "信息"
        
        return vm
    }()
    lazy var lineNameView : UIView = {
        let vi = UIView()
        vi.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        return vi
    }()
    lazy var whiteView : UIView = {
        let vi = UIView.init(frame: CGRect.init(x: 0, y: self.titleVm.frame.maxY, width: SCREEN_WIDHT, height: (kFitWidth(56)*2)))
        vi.backgroundColor = .white
        vi.isUserInteractionEnabled = true
        
        return vi
    }()
    lazy var oldNameLab : UILabel = {
        let lab = UILabel()
        lab.text = "设备名称"
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 14, weight: .medium)
        
        return lab
    }()
    lazy var oldNameLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK333
        lab.font = .systemFont(ofSize: 14, weight: .regular)
        lab.text = "\(oldName)"
        return lab
    }()
    lazy var newNameLab : UILabel = {
        let lab = UILabel()
        lab.text = "新的名称"
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 14, weight: .medium)
        
        return lab
    }()
    lazy var textField: UITextField = {
        let text = UITextField()
        text.placeholder = "请输入新的设备名称"
        text.textColor = .COLOR_TEXT_BLACK333
        text.delegate = self
        text.font = .systemFont(ofSize: 14, weight: .regular)
        return text
    }()
    lazy var lineView : UIView = {
        let vi = UIView()
        vi.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        return vi
    }()
    
    lazy var nextBtn : UIButton = {
        let btn = UIButton()
        btn.frame = CGRect.init(x: kFitWidth(31), y: SCREEN_HEIGHT - kFitWidth(100) - getBottomSafeAreaHeight(), width: kFitWidth(316), height: kFitWidth(48))
        btn.setTitle("确认修改", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.setBackgroundImage(UIImage.init(named: "button_theme_bg"), for: .normal)
        btn.layer.cornerRadius = kFitWidth(24)
        btn.clipsToBounds = true
        btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .regular)
        btn.backgroundColor = .clear
        
        btn.addTarget(self, action: #selector(confirmAction), for: .touchUpInside)
        
        return btn
    }()
}

extension DeviceSNUpdateNameVC{
    @objc func confirmAction() {
        if textField.text?.count ?? 0 == 0{
            MCToast.mc_text("请输入新的名称")
            return
        }
        sendUpdateNameRequest()
    }
}
extension DeviceSNUpdateNameVC:UITextFieldDelegate{
    func initUI() {
        initNavi(titleStr: "修改设备名称")
        
        view.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        
        view.addSubview(titleVm)
        
        view.addSubview(whiteView)
        whiteView.addSubview(lineNameView)
        whiteView.addSubview(oldNameLab)
        whiteView.addSubview(oldNameLabel)
        whiteView.addSubview(newNameLab)
        whiteView.addSubview(textField)
        whiteView.addSubview(lineView)
        
        view.addSubview(nextBtn)
        
        setConstrait()
    }
    func setConstrait() {
        lineNameView.snp.makeConstraints { make in
            make.left.top.width.equalToSuperview()
            make.height.equalTo(kFitWidth(1))
        }
        oldNameLab.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(24))
            make.top.equalToSuperview()
            make.height.equalTo(kFitWidth(56))
        }
        oldNameLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(121))
            make.top.height.equalTo(oldNameLab)
        }
        lineView.snp.makeConstraints { make in
            make.left.width.equalToSuperview()
            make.centerY.lessThanOrEqualToSuperview()
            make.height.equalTo(kFitWidth(1))
        }
        newNameLab.snp.makeConstraints { make in
            make.left.height.equalTo(oldNameLab)
            make.bottom.equalToSuperview()
        }
        textField.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(121))
            make.bottom.height.equalTo(newNameLab)
            make.right.equalTo(kFitWidth(-20))
        }
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string == ""{
            return true
        }
        if string == " "{
            return false
        }
        if textField.text?.count ?? 0 > 20{
            return false
        }
        
        return true
    }
}

extension DeviceSNUpdateNameVC{
    func sendUpdateNameRequest() {
        MCToast.mc_loading()
        let param = ["id":"\(deviceId)",
                     "posName":"\(textField.text ?? "")",
                     "userId":"\(UserModel.shared.mchUserIdForDevice)"]
        WHNetworkUtil.shareManager().POST(urlString: URL_Device_modify_name, parameters: param as [String : AnyObject],isNeedToast: true,vc: self) { responseObject in
            DLLog(message: "\(responseObject)")
            self.backTapAction()
        }
    }
}
